import 'package:cunyutech_flutter_ui/src/theme/main.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart' hide RefreshCallback;

import 'SliverListView.dart';
import 'SmilarListViewFooter.dart';

class SmilarScrollView extends StatefulWidget {
  SmilarScrollView({
    Key key, 
    this.child,
    this.renderRefresh, 
    this.onRefresh, 
    this.refreshIndicatorExtent = _defaultRefreshIndicatorExtent,
    this.refreshTriggerPullDistance = _defaultRefreshTriggerPullDistance,
    this.onEndReached, 
    this.endReachedTriggerDistance = _defaultEndReachedTriggerDistance,
    this.onEndReachedStateChange, 
    this.backgroundColor = Colors.transparent,
    this.footer, 
    this.header, 
    this.reverse = false,
  }) : super(key: key);

  /// 渲染下拉刷新器 可选
  final RefreshControlIndicatorBuilder renderRefresh;
  /// 内容
  final Widget child;
  /// 触发刷新事件 异步 可选
  final RefreshCallback onRefresh;
  /// 触发refresh零界点
  final double refreshIndicatorExtent;
  /// 触发refresh指示器的零界点
  final double refreshTriggerPullDistance;
  /// 触发上拉加载事件
  final EndReachedCallback onEndReached;
  // 触发endReached的零界点
  final double endReachedTriggerDistance;
  /// 列表尾部 可以渲染上拉加载组件
  final Widget footer;
  /// 头部
  final Widget header;
  /// 状态变更
  final EndReachedStateChange onEndReachedStateChange;
  /// 列表背景色，默认为透明色
  final Color backgroundColor;
  /// 是否反转
  final bool reverse;

  static const double _defaultRefreshTriggerPullDistance = 100.0;
  static const double _defaultRefreshIndicatorExtent = 60.0;
  static const double _defaultEndReachedTriggerDistance = 10;

  static SliverListViewState of(BuildContext context){
    return context.dependOnInheritedWidgetOfExactType<SliverListViewState>();
  }

  static defaultFooter(bool hasMore) {
    return SmilarListViewFooter(
        hasMore: hasMore == true, 
        textStyle: TextStyle(fontSize: CYFontSize.Default),
    );
  }

  _SmilarListViewState createState() => _SmilarListViewState();
}

class _SmilarListViewState extends State<SmilarScrollView> {
  EndReachedState reachedState;
  @override
  Widget build(BuildContext context) {

    List<Widget> listViewChildren = [];

    listViewChildren.add(widget.header);
    listViewChildren.add(widget.child);
    listViewChildren.add(widget.footer);

    return  SliverListViewState(
      reachedState: reachedState,
      context: context,
      child: CupertinoPageScaffold(
        backgroundColor: widget.backgroundColor,
        child: EndReachedNotificationListenerBuilder (
          endReachedTriggerDistance: widget.endReachedTriggerDistance,
          onEndReached: widget.onEndReached,
          onEndReachedStateChange: widget.onEndReachedStateChange,
          child: RefreshIndicator(
            onRefresh: widget.onRefresh != null ? widget.onRefresh : () async {},
            child: ListView(
              reverse: widget.reverse,
              controller: PrimaryScrollController.of(context),
              children: listViewChildren.where((item)=> item != null).toList(),
            ),
          )
        )
      )
    );
  }
}
